package com.chuanjiabao.wisdom.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.OSSClient;
import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.bean.FamilyTextCallbackBean;
import com.chuanjiabao.wisdom.databinding.ItemFamilyVideoBinding;
import com.chuanjiabao.wisdom.listener.OnItemClickListener;
import com.chuanjiabao.wisdom.util.GlideUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 86178 on 2019/8/24.
 * author mogen
 * 用心，成就一切。
 */

public class FamilyVideoAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder> {
    private List<FamilyTextCallbackBean.DataBeanX.DataBean> strings;
    private Context mcontext;
    OnItemClickListener mOnItemClickLitener;
    OSSClient oss;
    boolean isShow = false;

    List<TextView> lists = new ArrayList<>();

    public FamilyVideoAdapter(Context context, List<FamilyTextCallbackBean.DataBeanX.DataBean> strings
            ,OSSClient oss) {
        this.mcontext = context;
        this.strings = strings;
        this.oss = oss;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemFamilyVideoBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mcontext),
                R.layout.item_family_video, parent, false);
        binding.setAdapter(this);
        lists.add(binding.tvModuleSelect);
        return new TourViewHolder(binding);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, final int position) {
        holder.bindData(strings.get(position));
        final TourViewHolder tv = (TourViewHolder) holder;

        try {
            String url = oss.presignConstrainedObjectURL(
                    strings.get(position).getFile_bucket(),
                    strings.get(position).getVideo_img(),
                    30 * 60
            );

            GlideUtil.setUrlImg(
                    mcontext,
                    url,
                    strings.get(position).getVideo_img(),
                    tv.ivModule
            );
        } catch (ClientException e) {
            e.printStackTrace();
        }

        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(v -> {
                int pos = holder.getLayoutPosition();
                mOnItemClickLitener.onItemClick(holder.itemView, pos);
            });

            holder.itemView.setOnLongClickListener(v -> {
                int pos = holder.getLayoutPosition();
                mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                return false;
            });
        }
    }

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public int getItemCount() {
        return strings.size();
    }

    public class TourViewHolder extends BaseRecyclerViewHolder<FamilyTextCallbackBean.DataBeanX.DataBean> {
        ItemFamilyVideoBinding functionBinding;
        ImageView ivModule;
        TextView tvModuleSelect;

        public TourViewHolder(ItemFamilyVideoBinding dataBinding) {
            super(dataBinding.getRoot());
            functionBinding = dataBinding;
            ivModule = dataBinding.ivModule;
            tvModuleSelect = dataBinding.tvModuleSelect;
        }

        @Override
        public void bindData(FamilyTextCallbackBean.DataBeanX.DataBean data) {
            functionBinding.setBean(data);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return strings.size();
    }

    //选中状态操作
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void setChangePosition(int position) {
        if (isShow) {
            isShow = false;
            lists.get(position).setBackground(mcontext.getResources().getDrawable(R.drawable.module_draw_imge_select_no));
        }else{
            isShow = true;
            lists.get(position).setBackground(mcontext.getResources().getDrawable(R.drawable.module_draw_imge_right_and_top));
        }
    }

    //设置选中状态显示
    public void setTVShow() {
        for (TextView tv : lists) {
            tv.setVisibility(View.VISIBLE);
        }
    }

    //设置选中状态隐藏
    public void setTVGone() {
        for (TextView tv : lists) {
            tv.setVisibility(View.GONE);
        }
    }

}